﻿using System;
//using System.Threading;
using System.Windows.Forms;
using System.Drawing;

namespace Clock
{
	public partial class ClockForm : Form
	{
		//private Thread thread = null;
		private Point downPoint = Point.Empty;
		private delegate void SetTextCallback(string txtHour, string blink, string txtMin);
		private System.Timers.Timer timerGetTime;
		private bool bBlink = false;

		public ClockForm()
		{
			InitializeComponent();
			Init();
		}

		/*****************************************************************************************
		 * Function:	Init
		 * Description:	Create thread for display time
		 * Parameter:	None
		 * Return:		None
		 * Remark:		Should abort thread when application terminate.
		 *****************************************************************************************/
		private void Init()
		{
			//Thread to get 
			//thread = new Thread(new ThreadStart(Run));
			//thread.Start();

			// Transparent
			this.TransparencyKey = System.Drawing.Color.Turquoise;
			this.BackColor = System.Drawing.Color.Turquoise;

			// Remove from taskbar.
			ShowInTaskbar = false;

			// Screeen location
			int x = Screen.PrimaryScreen.Bounds.Right - this.Width;
			this.Location = new Point(x, 0);
			this.TopMost = true;

			//Timer
			timerGetTime = new System.Timers.Timer( 1000 );
			timerGetTime.Elapsed += new System.Timers.ElapsedEventHandler(timerGetTime_Elapsed);
			timerGetTime.Enabled = true; // Enable it
		}

		protected void HideForm(System.Object sender, System.EventArgs e)
		{
			this.Hide();
		}

		protected void DetailForm(System.Object sender, System.EventArgs e)
		{
			// ToDo
		}

		protected void CloseForm(System.Object sender, System.EventArgs e)
		{
			this.Close();
		}

		/*****************************************************************************************
		 * Function:	SetText
		 * Description:	Set text for hour, minis and blink label 
		 * Parameter:
		 * + txtHour [In]: string display for Hour label
		 * + blink 	[In]: string display for blink label
		 * + txtMin [In]: string display for Mins lablel 
		 * Return:		None
		 * Remark:		None
		 *****************************************************************************************/
		private void SetText(string txtHour, string blink, string txtMin)
		{
			// InvokeRequired required compares the thread ID of the
			// calling thread to the thread ID of the creating thread.
			// If these threads are different, it returns true.
			//if (this.lblHour.InvokeRequired)
			//{
			//	SetTextCallback d = new SetTextCallback(SetText);
			//	this.Invoke(d, new object[] { txtHour, blink, txtMin });
			//}
			//else
			{
				if( txtMin.Length == 1)
				{
					txtMin = "0" + txtMin;
				}

				this.lblHour.Text = txtHour;
				this.lblBlink.Text = blink;
				this.lblMin.Text = txtMin;
			}
		}

		/*****************************************************************************************
		 * Function:	Run
		 * Description:	Main procedure of thread
		 * Parameter:	None
		 * Return:		None
		 * Remark:		None
		 *****************************************************************************************/
		private void Run()
		{
			//bool bBlink = false;
			//string clockString = string.Empty;


			//while (true)
			//{
			//	SetText(DateTime.Now.Hour.ToString(), bBlink ? ":" : " ", DateTime.Now.Minute.ToString());
			//	Thread.Sleep(1000);
			//	bBlink = !bBlink;
			//}
		}

		private void timerGetTime_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
		{
			SetText(DateTime.Now.Hour.ToString(), bBlink ? ":" : " ", DateTime.Now.Minute.ToString());
			bBlink = !bBlink;
		}

		/*****************************************************************************************
		 * Function:	ClockForm_FormClosed
		 * Description:	Close thread after form closed
		 * Parameter:
		 * + sender [In]: object send closed event
		 * + e [In]: parameter event when form closed
		 * Return:		None
		 * Remark:		None
		 *****************************************************************************************/
		private void ClockForm_FormClosed(object sender, FormClosedEventArgs e)
		{
		//	thread.Abort();
		//	thread.Join();
		}

		private void clockForm_MouseDown(object sender, MouseEventArgs e)
		{
			if (e.Button == MouseButtons.Left)
			{
				downPoint = new Point(e.X, e.Y);

				// change mouse pointer
				this.Cursor = Cursors.NoMove2D;
			}
		}

		private void clockForm_MouseMove(object sender, MouseEventArgs e)
		{
			if (downPoint != Point.Empty)
			{
				Point location = new Point( this.Left + e.X - downPoint.X,
											this.Top + e.Y - downPoint.Y);

				this.Location = location;
			}
		}

		private void clockForm_MouseUp(object sender, MouseEventArgs e)
		{
			if (e.Button == MouseButtons.Left)
			{
				downPoint = Point.Empty;

				// change mouse pointer
				if( this.Cursor == Cursors.NoMove2D)
				{
					this.Cursor = Cursors.Default;
				}
			}
		}

		private void nIcon_DoubleClick(object sender, EventArgs e)
		{
			Show();
			WindowState = FormWindowState.Normal;
		}
	}
}
